package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.entity.ShoppingProduct;
import com.zhang.mapper.ShoppingProductMapper;
import com.zhang.service.ShoppingProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhangD
 * @since 2021-07-13
 */
@Service
public class ShoppingProductServiceImpl extends ServiceImpl<ShoppingProductMapper, ShoppingProduct> implements ShoppingProductService {

    @Autowired
    private  ShoppingProductMapper shoppingProductMapper;
    @Override
    public Page<ShoppingProduct> selectShoppingProductByCategory2Id(int category2Id, int pageNum , int pageSize) {
        Page<ShoppingProduct> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ShoppingProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("category2_id",category2Id);
        return shoppingProductMapper.selectPage(page, wrapper);

    }

    @Override
    public List<ShoppingProduct> selectShoppingProductByProductId(int productId) {
        HashMap<String, Object> productHashMap = new HashMap<>();
        productHashMap.put("product_id",productId);
        return shoppingProductMapper.selectByMap(productHashMap);
    }

    @Override
    public String addProduct(ProductVo productVo) {
        ShoppingProduct product = new ShoppingProduct();
        product.setProductName(productVo.getProductName());
        product.setCategory1Id(productVo.getCategory1Id());
        product.setCategory2Id(productVo.getCategory2Id());
        product.setPurchasePrice(productVo.getPurchasePrice());
        product.setSalePrice(productVo.getSalePrice());
        product.setInventory(productVo.getInventory());
        product.setImgSrc(productVo.getImgSrc());
        product.setDescription(productVo.getDescription());
        shoppingProductMapper.insert(product);
        return "插入成功";
    }

    @Override
    public String updateProduct(ProductVo productVo) {
        UpdateWrapper<ShoppingProduct> wrapper = new UpdateWrapper<>();
        wrapper.eq("product_id",productVo.getProductId());
        ShoppingProduct product = new ShoppingProduct();
        product.setProductName(productVo.getProductName());
        product.setCategory1Id(productVo.getCategory1Id());
        product.setCategory2Id(productVo.getCategory2Id());
        product.setPurchasePrice(productVo.getPurchasePrice());
        product.setSalePrice(productVo.getSalePrice());
        product.setInventory(productVo.getInventory());
        product.setImgSrc(productVo.getImgSrc());
        product.setDescription(productVo.getDescription());
        shoppingProductMapper.update(product,wrapper);
        return "修改成功";
    }


}
